//
//  FastLoginView.swift
//  NDAi
//
//  Created by 丸子 on 2024/6/3.
//

import UIKit
import YYText

class FastLoginView: UIView {
    
    lazy var titleLb: UILabel = {
        let label = UILabel()
        label.textColor = Theme.color.text_d1
        label.font = UIFont.pingFangSCRegular(ofSize: 16)
        label.text = "本机号码可以一键登录"
        return label
    }()
    
    lazy var descLb: UILabel = {
        let label = UILabel()
        label.textColor = Theme.color.text_d3
        label.font = UIFont.pingFangSCRegular(ofSize: 12)
        label.text = "认证服务由天翼数字生活账号提供"
        return label
    }()
    
    lazy var phoneLb: UILabel = {
        let label = UILabel()
        label.textColor = Theme.color.text_d1
        label.font = UIFont.pingFangSCBold(ofSize: 30)
        label.text = "132****4444"
        return label
    }()
    
    lazy var loginBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.titleLabel?.font = UIFont.pingFangSCRegular(ofSize: 16)
        button.setTitle("一键登录", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.allRound(22)
        button.backgroundColor = Theme.color.lineColor
        return button
    }()
    
    lazy var agreeBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "icon_ag_nor"), for: .normal)
        button.setImage(UIImage(named: "icon_login_ag"), for: .selected)
        return button
    }()
    
    lazy var textLabel: YYLabel = {
        let label = YYLabel()
        label.numberOfLines = 0
        label.textAlignment = .center
        // 创建富文本字符串
        let fullText = "我已阅读并同意《用户协议》《隐私政策》"
        let attributedString = NSMutableAttributedString(string: fullText)
        // 设置通用字体属性
        let fullRange = NSRange(location: 0, length: attributedString.length)
        attributedString.addAttributes([
            .font: UIFont.pingFangSCRegular(ofSize: 12), // 替换成你需要的字体
            .foregroundColor: Theme.color.text_d2 // 替换成你需要的颜色
        ], range: fullRange)
        // 设置《用户协议》的链接和颜色
        let userAgreementRange = (fullText as NSString).range(of: "《用户协议》")
        attributedString.yy_setTextHighlight(userAgreementRange, color: UIColor(hexString: "#45FFFF"), backgroundColor: nil) { _, _, _, _ in
            if let url = URL(string: "\(apiHost)/api/text/contract") {
                UIApplication.shared.open(url)
            }
        }
        // 设置《隐私政策》的链接和颜色
        let privacyPolicyRange = (fullText as NSString).range(of: "《隐私政策》")
        attributedString.yy_setTextHighlight(privacyPolicyRange, color: UIColor(hexString: "#45FFFF"), backgroundColor: nil) { _, _, _, _ in
            if let url = URL(string: "\(apiHost)/api/text/privacy") {
                UIApplication.shared.open(url)
            }
        }
        label.attributedText = attributedString
        return label
    }()
    //13888888886
    //1111
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubviews([titleLb, descLb, phoneLb, loginBtn, agreeBtn, textLabel])
        
        titleLb.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(36)
            make.centerX.equalToSuperview()
        }
        
        descLb.snp.makeConstraints { make in
            make.top.equalTo(titleLb.snp.bottom).offset(5)
            make.centerX.equalToSuperview()
        }
        
        phoneLb.snp.makeConstraints { make in
            make.top.equalTo(descLb.snp.bottom).offset(16)
            make.centerX.equalToSuperview()
        }
        
        loginBtn.snp.makeConstraints { make in
            make.top.equalTo(phoneLb.snp.bottom).offset(20)
            make.leading.trailing.equalToSuperview().inset(34)
            make.height.equalTo(44)
        }
        
        agreeBtn.snp.makeConstraints { make in
            make.top.equalTo(loginBtn.snp.bottom).offset(20)
            make.leading.equalToSuperview().offset(44)
            make.height.equalTo(24)
        }
        
        // 使用 SnapKit 设置 UITextView 的约束
        textLabel.snp.makeConstraints { make in
            make.centerY.equalTo(agreeBtn)
            make.leading.equalTo(agreeBtn.snp.trailing).offset(6)
            make.trailing.equalToSuperview().offset(-20)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
